/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managed;

import javax.ejb.EJB;
import javax.enterprise.context.Dependent;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.Length;
import session.PasswordForgottenBeanLocal;
import session.SignUpBeanLocal;

/**
 *
 * @author antonio
 */
@ManagedBean(name = "signUpController")
@RequestScoped        
@Dependent
public class SignUpControllerBean {
    
    
    private String username, password, rpassword, email, remail;
    
    private final String MSGUSR = "Username non disponibile";
    private final String MSGPWD = "Password non corrispondente";
    private final String MSGNOTNULL = "campo obbligatorio";
    private final String MSGMIN8 = "lunghezza minima 8 caratteri";
    private final String MSGMAX36 = "lunghezza massima 36 caratteri";
    private final String MSGMAX15 = "lunghezza massima 15 caratteri";
    
    @EJB
    private SignUpBeanLocal signUpBean;
    
    @EJB
    private PasswordForgottenBeanLocal passwordForgottenBean;
    
    private String usernameMessage = "";

    /**
     * Creates a new instance of SignUpControllerBean
     */
    public SignUpControllerBean() {
    }
    
    @NotNull(message=MSGNOTNULL)
    @Length(max=15, message=MSGMAX15)
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
    
    @NotNull(message=MSGNOTNULL)
    @Length(min=8, message=MSGMIN8)
    public String getPassword() {
        return password;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }
    
    @NotNull(message=MSGNOTNULL)
    public String getRpassword() {
        return rpassword;
    }

    public void setRpassword(String rpassword) {
        this.rpassword = rpassword;
    }
    
    @NotNull(message=MSGNOTNULL)
    @Length(max=36, message=MSGMAX36)
    public String getEmail() {
        return email;
    }
    
    public void setEmail(String email) {
        this.email = email;
    }
    
    @NotNull(message=MSGNOTNULL)
    public String getRemail() {
        return remail;
    }

    public void setRemail(String remail) {
        this.remail = remail;
    }

    public String getUsernameMessage() {
        return usernameMessage;
    }

    public void setUsernameMessage(String usernameMessage) {
        this.usernameMessage = usernameMessage;
    }
    
    public void signUp() {
        if(! signUpBean.signup(username, password, email)) {
            this.usernameMessage = MSGUSR;
        }
        else {
            this.usernameMessage = "Registrazione completata";
        }
    }
    
}
